home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 22 code / Paper Juggling / JuggleFiles.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-18  |  5.4 KB  |  208 lines  |  [TEXT/MMCC]

  1. #include "PaperJuggling.h"
  2. #include <StandardFile.h>
  3. #include <Folders.h>
  4.  
  5. // This routine just calls WriteJuggleToFile with the fsspec in the juggle
  6. OSErr SaveJuggle(JuggleHandle aJuggle)
  7. {
  8.     FSSpec        fileSpec;
  9.     
  10.     fileSpec = (*aJuggle)->fileSpec;
  11.     return WriteJuggleToFile(&fileSpec, aJuggle);
  12. }
  13.  
  14. // Man, it's a pain when people want to replace an existing file. Sheesh!
  15. OSErr JuggleToExistingFile(FSSpec *fileSpec, JuggleHandle aJuggle)
  16. {
  17.     OSErr    err;
  18.     unsigned long    tempLong;
  19.     short    tempRefNum;
  20.     Str255    tempName;
  21.     FSSpec    tempSpec;
  22.     FInfo    finderStuff;
  23.  
  24.     /* Make a unique file name */
  25.     GetDateTime(&tempLong);
  26.     NumToString(tempLong, tempName);
  27.     
  28.     /* locate the temporary items folder */
  29.     err = FindFolder(fileSpec->vRefNum, kTemporaryFolderType, kCreateFolder,
  30.                         &tempRefNum, (long *)&tempLong);
  31.     if(err == noErr)
  32.     {
  33.         /* Make a new FSSpec */
  34.         err = FSMakeFSSpec(tempRefNum, tempLong, tempName, &tempSpec);
  35.         if(err == noErr || err == fnfErr)
  36.         {
  37.             /* Create the temp file */
  38.             err = FSpCreate(&tempSpec, 'trsh', 'trsh', smSystemScript);
  39.             if(err == noErr)
  40.             {
  41.                 /* OK, we have a new temp file, let's write to it */
  42.                 err = WriteJuggleToFile(&tempSpec, aJuggle);
  43.                 if(err == noErr)
  44.                 {
  45.                     /* Lookin' good. Now, we need to exchange the data, set the Finder
  46.                         Info of the old (now new) file, and delete the temp file */
  47.                     err = FSpExchangeFiles(&tempSpec, fileSpec);
  48.                     if(err == noErr)
  49.                     {
  50.                         /* First get the existing finder Info */
  51.                         err = FSpGetFInfo(fileSpec, &finderStuff);
  52.                         if(err == noErr)
  53.                         {
  54.                             /* then set it to our type and creator */
  55.                             finderStuff.fdType = kJuggleFileType;
  56.                             finderStuff.fdCreator = kJuggleCreatorType;
  57.                             /* Don't care about this error: we did our best, by golly */
  58.                             FSpSetFInfo(fileSpec, &finderStuff);
  59.                         }
  60.                     }
  61.                 }
  62.                 /*     We successfully created the temp file, so now delete it,
  63.                     whatever else may have happened. If we can't, there's 
  64.                     nothing we can do about it, so this error is ignored */
  65.                 FSpDelete(&tempSpec);
  66.             }
  67.         }
  68.     }
  69.     
  70.     return err;
  71. }
  72.  
  73. /* It's much easier just to create a new file */
  74. OSErr JuggleToNewFile(FSSpec *fileSpec, JuggleHandle aJuggle)
  75. {
  76.     OSErr    err;
  77.  
  78.     err = FSpCreate(fileSpec, kJuggleCreatorType, kJuggleFileType, smSystemScript);
  79.     if(err == noErr)
  80.     {
  81.         err = WriteJuggleToFile(fileSpec, aJuggle);
  82.         /* If there were errors, delete the file */
  83.         if(err != noErr)
  84.             FSpDelete(fileSpec);
  85.     }
  86.     return err;
  87. }
  88.  
  89. // Writes the juggle to the given FSSpec
  90. OSErr WriteJuggleToFile(FSSpec *fileSpec, JuggleHandle aJuggle)
  91. {
  92.     JugglePointer    jugPtr;
  93.     OSErr            err;
  94.     long            writeSize;
  95.     short            refNum;
  96.     
  97.     HLock((Handle)aJuggle);
  98.     jugPtr = *aJuggle;
  99.     
  100.     /* First open the file */
  101.     err = FSpOpenDF(fileSpec, fsWrPerm, &refNum);
  102.     if(err == noErr)
  103.     {
  104.         // write numjugglers and numCounts out
  105.         writeSize = sizeof(short);
  106.         err = FSWrite(refNum, &writeSize, &jugPtr->numJugglers);
  107.         if(err == noErr)
  108.         {
  109.             writeSize = sizeof(short);
  110.             err = FSWrite(refNum, &writeSize, &jugPtr->numCounts);
  111.             if(err == noErr)
  112.             {
  113.                 // Write the block of throws
  114.                 writeSize = GetHandleSize((Handle)jugPtr->theThrows);
  115.                 HLock((Handle)jugPtr->theThrows);
  116.                 err = FSWrite(refNum, &writeSize, *jugPtr->theThrows);
  117.                 HUnlock((Handle)jugPtr->theThrows);
  118.             }
  119.         }        
  120.         /* Close the file. If there were errors, the file is deleted by the caller, who
  121.         created it in the first place */
  122.         FSClose(refNum);
  123.     }
  124.     
  125.     HUnlock((Handle)aJuggle);
  126.     return err;
  127. }
  128.     
  129. /* Opens the file indicated by fileSpec, reads the juggle in, 
  130. and creates a new document, showing the window. */
  131. OSErr OpenJuggleFile(FSSpec *fileSpec, WindowPtr *wind)
  132. {
  133.     OSErr            err;
  134.     long            readSize;
  135.     short            refNum, numJugglers, numCounts;
  136.     
  137.     /* Open the file and read in just the size (numJugglers and numCounts)
  138.     so we can get the size of the Juggle we'll need. */
  139.     err = FSpOpenDF(fileSpec, fsWrPerm, &refNum);
  140.     if(err == noErr)
  141.     {
  142.         /* Read in num jugglers */
  143.         readSize = sizeof(short);
  144.         err = FSRead(refNum, &readSize, &numJugglers);
  145.         if(err == noErr)
  146.         {
  147.             /* Read in num counts */
  148.             readSize = sizeof(short);
  149.             err = FSRead(refNum, &readSize, &numCounts);
  150.             if(err == noErr)
  151.             {
  152.                 // range check
  153.                 if(numJugglers > 0 && numJugglers <= kMaxJugglers &&
  154.                     numCounts > 0 && numCounts % 2 == 0)
  155.                 {
  156.                     WindowPtr    newWind;
  157.                     // !!! Check the file size
  158.                     
  159.                     // Set up the newJuggle
  160.                     newWind = AppNew(numJugglers, numCounts);
  161.                     if(newWind != nil)
  162.                     {
  163.                         JuggleHandle    aJuggle;
  164.                         HandHandle        throws;
  165.                         
  166.                         // Get the juggle, assume sucess
  167.                         aJuggle = GetWindowJuggle(newWind);
  168.                         throws = (*aJuggle)->theThrows;
  169.                         
  170.                         // read in throws data
  171.                         readSize = numCounts * numJugglers * sizeof(Hand);
  172.                         HLock((Handle)throws);
  173.                         err = FSRead(refNum, &readSize, *throws);
  174.                         HUnlock((Handle)throws);
  175.                         
  176.                         // data is restored. Now we need to do the bookkeeping
  177.                         // save the fileSpec and maybe the window
  178.                         (*aJuggle)->fileSpec = *fileSpec;
  179.                         if(wind != nil)
  180.                             *wind = newWind;
  181.                         
  182.                         /* Rename the window to match the file name */
  183.                         SetWTitle(newWind, fileSpec->name);
  184.                         
  185.                         /* clear the dirty flag */
  186.                         (*aJuggle)->dirty = false; /* no need to save unless it changes */
  187.                         
  188.                         // Finally, rebuild throws and  force a redraw
  189.                         RebuildThrowsPict(aJuggle);
  190.                         SetPort(newWind);
  191.                         EraseRect(&newWind->portRect);
  192.                         InvalRect(&newWind->portRect);
  193.                     }
  194.                 }
  195.             }
  196.         }
  197.         // Close the file
  198.         FSClose(refNum);
  199.     }
  200.     return err;
  201. }
  202.         
  203.         
  204.         
  205.         
  206.  
  207.  
  208.